package com.financing.wap.service.impl;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.financing.wap.dao.IActivitiesMollionsWealthDao;
import com.financing.wap.dto.ActivitesMollionsWealthDTO;
import com.financing.wap.dto.ProductDetailPageDTO;
import com.financing.wap.service.IActivitiesMollionsWealthService;
import com.financing.wap.service.IProductService;
import com.financing.wap.service.IProjectCompanyService;
import com.financing.wap.util.DateUtil;

@Service("activitesMollionsWealthService")
public class ActivitesMollionsWealthServiceImpl extends BaseServiceImpl implements IActivitiesMollionsWealthService{

	@Autowired
	private IActivitiesMollionsWealthDao activitiesMollionsWealthDao;
	
	@Autowired
	private IProductService productService;
	
	@Autowired
	private IProjectCompanyService projectCompanyServiceImpl;
	
	@Override
	public ActivitesMollionsWealthDTO queryActivitiesMollionsWealth(Integer pId,String pType)
			throws Exception {
		ActivitesMollionsWealthDTO dto = new ActivitesMollionsWealthDTO();
		dto.setpId(pId);
		dto.setpType(pType);
		return  activitiesMollionsWealthDao.queryActivitiesMollionsWealth(dto);
	}

	@Override
	public List<Map<String, Object>> queryActivitiesMollionsWealthList(
			ActivitesMollionsWealthDTO dto) throws Exception {
		
		List<Map<String,Object>> resList = new ArrayList<Map<String,Object>>();
		
		List<ActivitesMollionsWealthDTO> actM = activitiesMollionsWealthDao.queryActivitiesMollionsWealthList(dto);
		
		for(ActivitesMollionsWealthDTO amw : actM){
			Map<String,Object> res = new HashMap<String, Object>();
			//如果目前在产品活动时间中返回状态1:活动产品有效 如果目前还未到活动时间返回状态2：未开始 否则返回3:已结束
			Date activitiesStartDate = amw.getActivitiesStartDate();
			Date activitiesEndDate = amw.getActivitiesEndDate();
			if(DateUtil.nowBetweenstartTimeAndendTime(activitiesStartDate, activitiesEndDate)){
				amw.setStatus(1);
			}else if(DateUtil.nowBeforeStartTime(activitiesStartDate)){
				amw.setStatus(2);
			}else{
				amw.setStatus(3);
			}
			res.put("activitiesMollionsWealth", amw);
			if("05".equals(amw.getpType())){
				//基金
				
			}else{
				ProductDetailPageDTO pdpDto = productService.getProductDetailView(amw.getpId());
				Integer investOwnerId = pdpDto.getInvestOwnerId();
				if(investOwnerId != null){
					res.put("ProjectCompany", projectCompanyServiceImpl.getProjectCompany(investOwnerId));
				}
				res.put("ProductDetail",pdpDto);
			}
			resList.add(res);
		}
		
		return resList;
	}
	
	 		
}
